<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title>[[${sjcd}]]-[[${ejcd}]] - [[${T(com.b2c.erp.DataConfigObject).getInstance().getSysName()}]]</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">
<div class="layui-body" style="padding:15px;top: 0px;">
    <style type="text/css">
        .record-box h2 {
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 40px;
        }

        .record-input input {
            width: 400px;
            height: 22px;
            line-height: 22px;
            font-size: 16px;
            padding: 14px 9px;
            border: 1px solid #787878;
        }
        .record-box {
            position: relative;
            width: 900px;
            height: 550px;
            padding: 0 5px;
            font-size: 13px;
            color: #787878;
        }

        .record-head {
            margin: -20px 0 80px 0;
            height: 40px;
            line-height: 40px;
            font-size: 13px;
        }

        .record-head span {
            float: left;
        }

        .record-head i {
            float: right;
            font: normal 28px/30px Tahoma;
            cursor: pointer;
        }

        .record-code {
            position: absolute;
            left: 50%;
            top: 10px;
            margin-left: -110px;
            width: 220px;
            font-size: 14px;
            text-align: center;
        }

        .record-code img {
            width: 100%;
            display: block;
        }

        .record-code span {
            line-height: 2;
        }

        .record-box h2 {
            font-weight: normal;
            font-size: 20px;
            margin-bottom: 20px;
        }

        .record-info {
            line-height: 34px;
            overflow: hidden;
            margin-bottom: 15px;
        }

        .record-info span {
            float: left;
            width: 33.3%;
            text-align: left;
        }

        .record-info span:nth-child(3n-1) {
            padding-left: 9%;
            width: 35.66%;
        }

        .record-info span:nth-child(3n) {
            width: 20%;
        }

        .record-info span:last-child {
            width: 100%;
        }

        .record-table {
            max-height: 250px;
            overflow-y: auto;
        }

        .record-table table {
            width: 100%;
        }

        .record-table th, .record-table td {
            padding: 10px 0;
            font-size: 13px;
            text-align: center;
            border: 1px solid #787878;
        }

        .record-table th {
            padding: 5px 0;
        }

        .record-button {
            position: absolute;
            width: 100%;
            left: 0;
            bottom: 10px;
            text-align: center;
        }

        .record-button a {
            border: 1px solid #787878;
            padding: 10px 15px;
            font-size: 14px;
            color: #787878;
        }
        .quantity {
            border: 1px solid #787878;
        }
        .red{
            border: 1px solid #ff0000;
        }
        .sel {
            background-color: #00FF00;
        }
    </style>


    <link rel="stylesheet" href="/sb/stock.css"/>
    <fieldset class="layui-elem-field">
        <legend>扫码操作</legend>
        <div class="layui-field-box">
            <form class="layui-form layui-form-pane1" >
                <div class="layui-form-item">

                    <div class="layui-inline">
                        <input type="text" id="num" name="num" placeholder="商品SKU编码" class="layui-input" style="width: 260px;">
                    </div>


                    <div class="layui-inline">
                        <button lay-submit="" class="layui-btn" lay-filter="scanCode">添加</button>
<!--                        <i>总共扫码：<span th:text="${totalSize}" id="totalCount">0</span>&nbsp;条</i>-->
<!--                        <i>出库成功：<span th:text="${totalSize}" id="successCount" style="color: red">0</span>&nbsp;条</i>-->
                    </div>
                    <div class="layui-inline"></div>

                </div>
            </form>



        </div>
    </fieldset>
    <h2 class="sp-title"><span>入库详情</span></h2>

    <form id="stockinform" action="/purchase/stock_in" method="post">

        <div class="sp-table">

            <input type="hidden" name="invoiceId" id="invoiceId" th:value="${invoice.id}" />
            <input type="hidden" name="invoiceNo" th:value="${invoice.billNo}" />
            <table>
                <thead>
                <tr>
                    <th>序号</th>
                    <th>商品</th>
                    <th>规格</th>
                    <th>SKU</th>
                    <th>数量</th>
                    <th>已入库数量</th>
                    <th>入库仓位</th>
                    <th>入库数量</th>

                </tr>
                </thead>
                <tbody id="sp-data">
                <tr th:each="item:${invoice.items}">
                    <td th:text="${itemStat.index + 1}"></td>
                    <td th:text="${item.goodsName}">
                    </td>
                    <td>
                        颜色：<i th:text="${item.colorValue}"></i>&nbsp;尺码：<i th:text="${item.sizeValue}"></i>
                    </td>
                    <td th:text="${item.specNumber}"></td>
                    <td th:text="${item.quantity}">200</td>
                    <td th:text="${item.inQuantity}">0</td>
                    <!--                <td>-->
                    <!--                    <span th:if="${item.inQuantity==item.quantity}">已全部入库</span>-->
                    <!--                    <span th:if="${item.inQuantity==0}">待入库</span>-->
                    <!--                    <span th:if="${item.inQuantity<item.quantity && item.inQuantity>0}">部分入库</span>-->
                    <!--                </td>-->
                    <td>
                        <input th:if="${#lists.isEmpty(item.getStocks())}" type="text" class="locationNumber" name="locationNumber" placeholder="请输入仓位编码" autocomplete="off"/>
                        <input th:if="${#lists.isEmpty(item.getStocks())}" type="hidden" class="locationId" name="locationId"  value="0" />
                        <select th:unless="${#lists.isEmpty(item.getStocks())}" name="locationId" class="locationId" >
                            <option th:each="sitem:${item.getStocks()}" th:value="${sitem.getLocationId()}" th:text="${sitem.getLocationName()}"></option>
                        </select>
                        <input type="hidden" name="infoId"  th:value="${item.id}" />
                        <input type="hidden" name="specId"  th:value="${item.specId}" />
                        <input type="hidden" name="specNumber"  th:value="${item.specNumber}" />
                        <input type="hidden" name="goodsNumber"  th:value="${item.goodsNumber}" />
                        <input type="hidden" name="price"  th:value="${item.getPrice()}" />

                        <input type="hidden" name="maxQuantity"  th:value="${item.quantity - item.inQuantity}" />
                        <span th:if="${item.specId==null||item.specId==0}" style="color: red">商品规格数据不存在</span>

                        <!--                    <i th:if="${item.specId!=null&&item.specId>0 && item.inQuantity < item.quantity}" class="enter" th:attr="data-id=${item.specNumber}">选择仓库</i>-->
                    </td>
                    <td>
                        <input type="number" name="quantity" class="quantity" th:classappend="${item.specNumber}" th:value="${item.quantity - item.inQuantity}" autocomplete="off" placeholder="入库数量" th:attr="maxQty=${item.quantity - item.inQuantity}" value="0" readonly/>
                    </td>
                </tr>

                </tbody>
            </table>

        </div>
        <div class="check-box">
            <div class="record-info">
                <span>采购单号：<i id="r-number" th:text="${invoice.billNo}"></i></span>
                <span>采购单日期：<i id="r-track" th:text="${invoice.billDate}"></i></span>
                <span></span>
                <span>采购总数：<i id="r-company" th:text="${invoice.totalQuantity}"></i>&nbsp;</span>
                <span>已入库：<i id="r-time" style="color: red" th:text="${invoice.inQuantity}"></i></span>
                <span></span>
            </div>
            <div class="sp-tool">
                    <span class="fl">
                        <b><select name="qcReport" id="qcReport">
                                <option value="" disabled>是否有QC报告</option>
                                <option value="0" >没有QC报告</option>
                                <option value="1" >有QC报告</option>
                            </select>
                        </b>
                        <b>QC交接人：
                            <input type="text" name="qcInspector" placeholder="QC交接人" class="date ui-input ui-datepicker-input" autocomplete="off"></b>

                    </span>
                    <span class="fl">
                        <b style="color: red;" th:text="${msg}"></b>
                    </span>
                    <span class="fr">
                        <i th:if="${T(com.b2c.entity.erp.enums.InvoiceBillStatusEnum).Audited.index == invoice.billStatus}" class="save" id="save">确认入库</i>
                    </span>
            </div>
            <div class="sp-tool">


            </div>
        </div>
    </form>
    <script>
        layui.use(['laydate', 'layer','form'], function () {
            var laydate = layui.laydate;
            var layer = layui.layer;
            var form = layui.form;

            form.on('submit(scanCode)', function (obj) {
                var num = $("#num").val();
                $("#num").val('');
                $("#num").focus();
                if (num == "") {
                    alert("请输入商品SKU编码")
                    return false;
                }
                //判断是否存在
                var spec_arr =[];
                $("input[name='specNumber']").each(function(){
                    spec_arr.push($(this).val().toUpperCase());
                })

                if(spec_arr.indexOf(num.toUpperCase()) < 0){
                    alert("SKU:"+num+"不在该采购表单");
                    return false;
                }

                
                var obj = $("."+num.toUpperCase());

                if(obj.is(":empty") == false) {
                    obj = $("."+num.toLowerCase());
                }
                
                var h = $(obj).val();
                h++;
                var maxQty = $(obj).attr("maxQty");

                if(parseInt(h) > parseInt(maxQty)){
                    alert('已经超过可入库最大数量');
                }else{$(obj).val(h);}

                $(obj).addClass("red");
                $(obj).parent().parent().addClass("sel");
                setTimeout(function(){
                    $(obj).removeClass("red");
                    $(obj).parent().parent().removeClass("sel");
                },600);
                return false;
            });

        });

        $(function () {

            $("#num").val();
            $("#num").focus();


            // 保存
            $('#save').click(function () {
                var btnObj = $(this);
                var hasClick = $(this).hasClass("hasClick");
                // alert(hasClick);
                if(hasClick == true){
                    alert("保存中，请勿重复点击");
                    return ;
                }else{
                    btnObj.addClass("hasClick");
                }


                //判断入库总数不能为0
                var inQty = 0;
                $(".quantity").each(function () {
                    var inq = $(this).val();
                    if(isNaN(parseInt(inq) )){
                        inq = 0;
                    }
                    inQty += parseInt(inq);
                })

                if(inQty <= 0){
                    alert("没有要入库的SKU");
                    btnObj.removeClass("hasClick");
                    return;
                }


                var qcReport = $("#qcReport").val();
                if (qcReport == '') {
                    alert("请确认是否有QC报告");
                    return false;
                }

                $.ajax({
                    url: "/ajax_stock_in/purchase_stock_in_data",
                    type: "POST",
                    dataType: "json",
                    data: $('#stockinform').serialize(),
                    success: function (res) {
                        if (res.code == 0) {
                            location.href='/stock_in/purchase_in_wait_list';
                        } else {
                            alert(res.msg);
                        }
                    }
                });

               /* var stockInItems = [];//要提交的入库数据
                var invoiceId = $("#invoiceId").val();
                var returnResult = true;//是否有错误数据
                var totalQuantity = 0;//总入库数量
                var locationNumbers = [];//入库填写的仓位
                //检查仓位
                $("input[name^='locationNumber']").each(function (i,el) {

                    var locationNumber = $(this).val();
                    var locationId = $("input[name^='locationId']").eq(i).val();
                    var quantity = $("input[name^='quantity']").eq(i).val();

                    var infoId = $("input[name^='infoId']").eq(i).val();
                    var specId = $("input[name^='specId']").eq(i).val();
                    var specNumber = $("input[name^='specNumber']").eq(i).val();

                    if (locationNumber != "" ) {
                        if(locationNumbers.indexOf(locationNumber) > -1){
                            btnObj.removeClass("hasClick");
                            alert("不能入库到相同的仓位");
                            returnResult = false;
                            return;
                        }
                        locationNumbers.push(locationNumber);
                        //选中入库，判断数量是否填写
                        if(quantity == "" || Number(quantity) <= 0 ){
                            btnObj.removeClass("hasClick");
                            alert("请输入入库数量");
                            $("input[name^='quantity']").eq(i).focus();
                            returnResult = false;
                            return;
                        }else{
                            //检查可入库数量
                            var maxQuantity = $("input[name^='maxQuantity']").eq(i).val();
                            if(Number(quantity) > Number(maxQuantity)){
                                btnObj.removeClass("hasClick");
                                alert("超出可入库数量");
                                $("input[name^='quantity']").eq(i).focus();
                                returnResult = false;
                                return;
                            }else {
                                totalQuantity += Number(quantity);
                                var item = {
                                    locationId:locationId,
                                    locationNumber:locationNumber,
                                    invoiceInfoId:infoId,
                                    specId:specId,
                                    specNumber:specNumber,
                                    quantity:Number(quantity)
                                }
                                stockInItems.push(item);
                            }
                        }

                    }
                })

                if (returnResult == false) {
                    btnObj.removeClass("hasClick");
                    return false;
                }
                if (totalQuantity == 0) {
                    btnObj.removeClass("hasClick");
                    alert("没有要入库的商品");
                    return false;
                }

                var data = {
                    invoiceId:invoiceId,
                    items:stockInItems
                }

                //检查入库仓位等校验
                $.ajax({
                    url: "/ajax_stock_in/purchase_stock_in_data_checked",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (res) {
                        btnObj.removeClass("hasClick");
                        if (res.code == 0) {
                            $("#stockinform").submit();
                        } else {
                            alert(res.msg);
                        }
                    }
                });*/

                return false;
            });
        });
    </script>


</div>
</body>
</html>